package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.os.Bundle;

import com.example.myapplication.R;

import android.os.CountDownTimer;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.utils.Locus;
import com.example.myapplication.utils.Point;

import java.util.ArrayList;
import java.util.List;


public class MapActivity3 extends AppCompatActivity {

    private ImageButton pos;
    private ImageButton search;
    private Button invisible;
    private EditText e_shopname;
    private String shopname;
    private MyView local;
    private Point point;
    private ImageView imageView;
    private Bitmap bitmap;
    private Button yf501;
    private Button yf502;
    private Button yf503;
    private Button yf504;
    private Button yf505;
    private Button yf507;
    private Button yf508;
    private Button yf509;
    private Button yf510;
    private Button yf512;
    private Button yf513;
    private Button yf514;
    private Button cancel;
    private Locus track;
    private int floor=5;
    private int start;
    private int end;

    float x;
    float y;

    private static final int UPDATE_P = 1;
    private Handler handler;
    private Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map3);
        local = findViewById(R.id.myView);
        invisible = (Button) findViewById(R.id.invisibleMap3);
        init();
        /*handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case UPDATE_P:
                        // 在这里可以进行UI操作
                        updateUI();
                        break;
                    default:
                        break;
                }

            }
        };

        // 创建Runnable实例
        runnable = new Runnable() {
            @Override
            public void run() {
                // 发送消息到Handler
                Message message = new Message();
                message.what = UPDATE_P;
                handler.sendMessage(message);

                // 每隔0.5秒重复执行
                handler.postDelayed(this, 500);
            }
        };

        // 开始执行Runnable
        handler.postDelayed(runnable, 500);*/
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if(itemId==R.id.f3_item){
            Intent intent1=new Intent(MapActivity3.this,MapActivity2.class);
            startActivity(intent1);
        } else if (itemId==R.id.f4_item) {
            Intent intent2=new Intent(MapActivity3.this,MapActivity1.class);
            startActivity(intent2);
        }
        return true;
    }

    /*private void updateUI() {
        // 在这里执行UI的更新操作
        float x = local.getWidth();
        float y = local.getHeight();

        point.getLocation(new Point.LocationCallback() {
            @Override
            public void onLocationResult(float[] location) {
                if (location.length != 0) {
                    float new_x = (location[0]/230)*x;
                    float new_y = (location[1]/680)*y;
                    local.setUserLocation(new PointF(new_x,new_y));
                }
            }

            @Override
            public void onLocationError(Exception e) {
                System.out.println(e);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 移除Runnable的回调，以防止内存泄漏
        handler.removeCallbacks(runnable);
    }*/

    private void init(){
        point = new Point(this,this);
        pos=(ImageButton)findViewById(R.id.select_p01);
        search = (ImageButton)findViewById(R.id.select_p02);
        cancel=(Button)findViewById(R.id.cancel3);
        yf501 = (Button) findViewById(R.id.YF501);
        yf502 = (Button) findViewById(R.id.YF502);
        yf503 = (Button) findViewById(R.id.YF503);
        yf504 = (Button) findViewById(R.id.YF504);
        yf505 = (Button) findViewById(R.id.YF505);
        yf507 = (Button) findViewById(R.id.YF507);
        yf508 = (Button) findViewById(R.id.YF508);
        yf509 = (Button) findViewById(R.id.YF509);
        yf510 = (Button) findViewById(R.id.YF510);
        yf512 = (Button) findViewById(R.id.YF512);
        yf513 = (Button) findViewById(R.id.YF513);
        yf514 = (Button) findViewById(R.id.YF514);

        //按钮与搜索框相连
        e_shopname=(EditText)findViewById(R.id.select01);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<PointF> mylocus=new ArrayList<>();
                local.setNavigationPath(mylocus);
            }
        });
        yf501.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("周大福");
                end=0;
                // e_shopname.invalidate();
            }
        });

        yf502.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("花点时间");
                end=2;
            }
        });

        yf503.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("JNBY");
                end=3;
            }
        });

        yf504.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("九木杂物社");
                end=10;
            }
        });

        yf505.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("APPLE");
                end=11;
            }
        });

        yf507.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("全棉时代");
                end=11;
            }
        });

        yf508.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("泡泡玛特");
                end=10;
            }
        });

        yf509.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("LOHO");
                end=8;
            }
        });

        yf510.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("丝芙兰");
                end=6;
            }
        });

        yf512.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("MAC");
                end=3;
            }
        });

        yf513.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("乐高");
                end=2;
            }
        });

        yf514.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("小米");
                end=0;
            }
        });


        invisible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CountDownTimer timer = new CountDownTimer(1000000000, 2000) {
                    float x = local.getWidth();
                    float y = local.getHeight();
                    @Override
                    public void onTick(long l) {


                        point.getLocation(new Point.LocationCallback() {
                            @Override
                            public void onLocationResult(float[] location) {
                                if (location != null && location.length != 0) {
                                    System.out.println("X:"+x+" Y:"+y);
                                    float new_x = (location[0]/230)*x;
                                    float new_y = (location[1]/680)*y;
                                    System.out.println("local[0]:"+location[0]+"local[1]: "+location[1]);
                                    start = (int)location[2];
                                    local.setUserLocation(new PointF(new_x,new_y));
                                }
                            }

                            @Override
                            public void onLocationError(Exception e) {
                                System.out.println(e);
                            }
                        });
                    }

                    @Override
                    public void onFinish() {

                    }
                }.start();

            }
        });

        /*List<PointF> s=new ArrayList<>();
        PointF a=new PointF(20,30);
        PointF b=new PointF(30,40);
        PointF c=new PointF(40,50);
        s.add(a);
        s.add(b);
        s.add(c);*/
        pos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                track=new Locus(floor,start,end);
                //local.setNavigationPath(s);
                CountDownTimer timer = new CountDownTimer(1000000000, 2000) {
                    float x = local.getWidth();
                    float y = local.getHeight();
                    @Override
                    public void onTick(long l) {
                        track.getLocation(new Locus.LocationCallback() {
                            @Override
                            public void onLocationResult(List<PointF> locus) {
                                List<PointF> mylocus=new ArrayList<>();
                                int size = locus.size();
                                for (int i = 0; i < size; i++) {
                                    PointF p = locus.get(i);
                                    PointF myp=new PointF((p.x / 230) * x,(p.y / 680) * y);
                                    mylocus.add(myp);
                                }
                                local.setNavigationPath(mylocus);
                            }

                            @Override
                            public void onLocationError(Exception e) {
                                System.out.println(e);
                            }
                        });
                    }

                    @Override
                    public void onFinish() {
                    }
                }.start();
            }

        });

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //搜索商家信息
                shopname=e_shopname.getText().toString().trim();
                Intent intent = new Intent(MapActivity3.this,activity_info.class);
                intent.putExtra("storename",shopname);
                startActivity(intent);

            }
        });

        ViewTreeObserver vto = local.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 在视图树完成测量时获取宽度和高度
                x = local.getWidth();
                y = local.getHeight();
                // 确保只会调用一次
                local.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                // 在需要触发按钮事件的地方调用 performClick()
                invisible.performClick();
            }
        });

    }

}